package db_load;

import static java.lang.System.out;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Load {
	static final String JDBC_DRIVER = "org.sqlite.JDBC";
	static final String DB_URL = "jdbc:sqlite:d:/db/sqlite/test.db";

	public static void main(String[] args) {

		try {
			Class.forName(JDBC_DRIVER);
		} catch (ClassNotFoundException ex) {
			out.println(ex);
		}

		String sql = "INSERT INTO emp2 VALUES (?, ?, ?, ?, ?, ?, ?,?)";
		try (Connection con = DriverManager.getConnection(DB_URL);
				PreparedStatement ps = con.prepareStatement(sql);) {

			Contacts.read();
			for (Contacts c : Contacts.getContactsList()) {

				ps.setInt(1, c.id);
				ps.setString(2, c.userid);
				ps.setString(3, c.fname);
				ps.setString(4, c.lname);
				ps.setString(5, c.email);
				ps.setString(6, c.country);
				if (c.age == null) {
					ps.setNull(7, java.sql.Types.NULL);
				}else {
					ps.setInt(7, c.age);
				}
				ps.setString(8, c.phone);

				ps.executeUpdate();
			}
		} catch (SQLException ex) {
			out.println(ex);
		}
	}
}
